<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function loadImg() {
        return new Promise((res, rej) => {
          //   let src =
          //     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242332225H9-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642643996&t=9f9b70fd8391a0eee084e3e436b1ab3f";
          let src = "https://gimg2.baidu";
          let img = new Image();
          img.src = src;
          img.onload = function () {
            res(this);
          };
          img.onerror = function () {
            console.log("加载失败");
            rej("加载失败1");
          };
        });
      }
      // catch 可以把整个then链上的错误捕捉到 然后输出；
      loadImg()
        .then((res) => {
          document.body.appendChild(res);
        })
        .catch((err) => {
          console.log(err);
          //加载失败1
        });
    </script>
  </body>
</html>
